home *** CD-ROM | disk | FTP | other *** search
/ MacFormat España 26 / macformat_26.iso / Shareware / Programación / C Reference Card / C Reference Card.rsrc / TEXT_405_5.txt < prev    next >
Text File  |  1997-01-29  |  6KB  |  232 lines

  1. FUNCTIONS : basics, overloading, command line arguments, C and C++, ...
  2. _________________________________________________________________________
  3.  
  4.  
  5. FUNCTION BASICS
  6.  
  7. Almost all programs use functions to break-up large programs into manageable and reusable chunks of code.  Related functions are usually grouped into separate files which are independently compiled.  When using functions from a separate file or library, you must use the "extern" keyword followed by the function prototype to tell the compiler that the particular function is not part of the file.  The following is an example of how to call functions from a separate file:
  8.  
  9.   // file1.cp
  10.   #include <iostream.h>
  11.  
  12.   // function prototypes
  13.   extern float myInput( char *prompt );
  14.   extern float myMultiply( float a, float b );
  15.   extern void myPrintResult( float result );
  16.  
  17.   void main( void )
  18.   {
  19.     float a,b,result;
  20.  
  21.     a = myInput( "Input A: " );
  22.     b = myInput( "Input B: " );
  23.     result = myMultiply( a, b );
  24.     myPrintResult( result );
  25.   }
  26.   // end file1.cp
  27.  
  28.  
  29.   // file2.cp
  30.   #include <iostream.h>
  31.  
  32.   // function prototypes
  33.   float myInput( char *prompt );
  34.   float myMultiply( float a, float b );
  35.   void myPrintResult( float result );
  36.  
  37.   float myInput( char *prompt )
  38.   {
  39.     float input;
  40.  
  41.     cout << prompt;
  42.     cin >> input;
  43.   
  44.     return input;
  45.   }
  46.  
  47.   float myMultiply( float a, float b )
  48.   {
  49.     return a * b;
  50.   }
  51.  
  52.   void myPrintResult( float result )
  53.   {
  54.     cout << "The result is ";
  55.     cout << result << endl;
  56.   }
  57.   // end file2.cp
  58.  
  59.  
  60.  
  61. FUNCTION OVERLOADING
  62.  
  63. In C++, you can create more than one function with the same name.  However, the parameter list of these "overloaded" functions must differ.  When asked to call a function, the compiler will compare the parameters of the called function to the list of functions which share the same name.  When a match is determined, the proper function is called.
  64.  
  65.   // overload.cp
  66.   #include <iostream.h>
  67.  
  68.   void DoAdd( int a, int b );
  69.   void DoAdd( float a, float b );
  70.  
  71.   void main( void )
  72.   {
  73.     int i1   =   5;
  74.     int i2   =  10;
  75.     float f1 = 2.5;
  76.     float f2 = 4.1;
  77.  
  78.     DoAdd( i1, i2 );
  79.     DoAdd( f1, f2 );
  80.   }
  81.  
  82.   void DoAdd( int a, int b )
  83.   {
  84.     int result;
  85.  
  86.     result = a + b;
  87.     cout << a << " + " << b << " = " << result << endl;
  88.   }
  89.  
  90.   void DoAdd( float a, float b )
  91.   {
  92.     float result;
  93.  
  94.     result = a + b;
  95.     cout << a << " + " << b << " = " << result << endl;
  96.   }
  97.   // end overload.cp
  98.  
  99.  
  100.  
  101. COMMAND LINE ARGUMENTS
  102.  
  103. Although the Macintosh doesn't utilize a command line prompt (at least not yet), you may need to write programs which use command line arguments.  The following code should work with most Macintosh compilers:
  104.  
  105.   // command.cp
  106.   #include <iostream.h>
  107.   #include <console.h>          // compiler-specific
  108.  
  109.   int main( int argc, char *argv[] )
  110.   {
  111.     int i;
  112.  
  113.     argc = ccommand( &argv );   // compiler-specific
  114.  
  115.     for( i = 1; i < argc; i++ )
  116.       cout << argv[i] << " ";
  117.     cout << endl;
  118.  
  119.     return 0;
  120.   }
  121.   // end command.cp
  122.  
  123.  
  124.  
  125. CALLING C FUNCTIONS FROM C++
  126.  
  127. C++ performs "name mangling" in order to perform such feats as function overloading.  Basically, this means that C++ renames your functions behind the scenes.  Therefore, if you need to call a C function (this usually means calling a function from an object code library which has been compiled by a straight C compiler), you need to let the C++ compiler know not to perform any name mangling.  This is performed by using the following statement:
  128.  
  129.   extern "C" function_prototype;
  130.  
  131. If there is a list of functions, you can use:
  132.  
  133.   extern "C"
  134.   {
  135.     function_prototype1;
  136.     function_prototype2;
  137.     function_prototype3;
  138.     //...
  139.   }
  140.  
  141. To enclose a complete library, you can use:
  142.  
  143.   extern "C"
  144.   {
  145.     #include <header_file.h>
  146.   }
  147.  
  148.  
  149.  
  150. DEFAULT ARGUMENT LIST
  151.  
  152. You can supply default values for function arguments in C++ as follows:
  153.  
  154.   // default.cp
  155.   #include <iostream.h>
  156.  
  157.   void myPrint( int val = 0, int times = 1 );
  158.  
  159.   void main( void )
  160.   {
  161.     myPrint();
  162.     myPrint(5);
  163.     myPrint(7,3);
  164.   }
  165.  
  166.   void myPrint( int val, int times )
  167.   {
  168.     for( int i=1; i<=times; i++ )
  169.     {
  170.       if( i==1 )
  171.         cout << "Output Value: " << val << endl;
  172.       else
  173.         cout << "      Copy " << i << ": " << val << endl;
  174.     }
  175.   }
  176.   // end default.cp
  177.  
  178.  
  179.  
  180. VARIABLE-LENGTH ARGUMENT LIST
  181.  
  182. Although not recommended, you may need to create a function which has a variable argument list (such as printf and scanf).  Here's how to define and use variable-length argument lists in functions:
  183.  
  184.   // list.cp
  185.   #include <iostream.h>
  186.   #include <stdarg.h>
  187.  
  188.   void myPrintList( char *format, ... );  // ellipsis must be last item
  189.  
  190.   void main( void )
  191.   {
  192.     int    int1  = 2;
  193.     int    int2  = 7;
  194.     double real1 = 3.14;
  195.   
  196.     myPrintList( "List:%d%d%f", int1, int2, real1 );
  197.   }
  198.  
  199.   void myPrintList( char *format, ... )
  200.   {
  201.     va_list argPtr;
  202.     char    *p;
  203.     int     intItem;
  204.     double  realItem;
  205.  
  206.     va_start( argPtr, format );
  207.     for( p = format; *p; p++ )  // search for end of string
  208.     {
  209.       if( *p != '%' )
  210.       {
  211.         cout << *p; // print char
  212.         continue;   // get next letter
  213.       }
  214.       cout << endl;
  215.       switch( *++p )
  216.       {
  217.         case 'd':
  218.           intItem = va_arg( argPtr, int );
  219.           cout << intItem;
  220.           break;
  221.         case 'f':
  222.           realItem = va_arg( argPtr, double );
  223.           cout << realItem;
  224.           break;
  225.         default:
  226.           cout << "%" << *p;
  227.           break;
  228.       }
  229.     }
  230.     va_end( argPtr ); // clean up
  231.   }
  232.   // end list.cp